#include "common.h"
#include "i2c2.h"
#include "sht20.h"

int sht20::sht20_read(float* tempori,float* humiori)
{
    int ret;
    //读取温度传感器数据
    unsigned char temporiBuf[3]; //温度传感器
    ret = i2c_read(SHT20SLAVEADDR,TRIG_T_MEASUREMENT_HM,temporiBuf);
    if (ret != 0) {
        printf("atlas_i2c_read sht20 fail!\n");
        return -1;
    }
    //解析温度数据
    temporiBuf[1] = temporiBuf[1] & 0xFC; 
    unsigned short temp_tempori = (temporiBuf[0]<<8)|temporiBuf[1];
    *tempori = -46.58+(175.75*temp_tempori)/(1<<16);


    //读取温度传感器数据
    unsigned char humioriBuf[3]; //湿度传感器
    ret = i2c_read(SHT20SLAVEADDR,TRIG_RH_MEASUREMENT_HM,humioriBuf);
    if (ret != 0) {
        printf("atlas_i2c_read sht20 fail!\n");
        return -1;
    }
    //解析湿度数据
    humioriBuf[1] = humioriBuf[1] & 0xFC;
    unsigned short temp_humiori = (humioriBuf[0]<<8)|humioriBuf[1];
    *humiori = -6.0+(125.0*temp_tempori)/(1<<16);
    return 0;
}

sht20::sht20(/* args */)
{
}

sht20::~sht20()
{

}